unit QueryUtils;

interface
uses
  SysUtils, Classes, DB, DBGRids, IniFiles;

procedure LoadGridSettings(Grid: TDBGrid; QueryName: String);
procedure SaveGridSettings(Grid: TDBGrid; QueryName: String);

implementation
uses
  ProgramM;

procedure LoadGridSettings(Grid: TDBGrid; QueryName: String);
var
  i : Integer;
  Section: String;
  Col : TColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  Section := QueryName + 'Columns';
  Options.ReadSection(Section, FieldNames);
  
  for i := 0 to FieldNames.Count-1 do
  begin
    Col := Grid.Columns.Add;
    Col.FieldName := FieldNames[i];
    Col.Width := Options.ReadInteger(Section, Col.FieldName, 100);
  end;

  FieldNames.Free;
end;

procedure SaveGridSettings(Grid: TDBGrid; QueryName: String);
var
  i : Integer;
  Section: String;
  Col: TColumn;
begin
  if (Grid.DataSource = nil) or (Grid.DataSource.DataSet = nil)
    or not Grid.DataSource.DataSet.Active then
  begin
    Exit;
  end;
  
  Section := QueryName + 'Columns';
  Options.EraseSection(Section);

  for i := 0 to Grid.Columns.Count-1 do
  begin
    Col := Grid.Columns[i];
    Options.WriteInteger(Section, Col.FieldName, Col.Width);
  end;
end;

end.
